//
//  CFAlertView.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/5/2.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "CFAlertView.h"

@implementation CFAlertView

+(void)showToView:(UIView *)superView title:(NSString *)title message:(NSString *)message corfirme:(corfirmeBlock)block{
     CFAlertView * alertView = [[CFAlertView alloc] initWithTitle:title message:message frame:superView.bounds corfirme:block];
    [superView addSubview:alertView];
}

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message frame:(CGRect)frame corfirme:(corfirmeBlock)block{
    if (self = [super init]) {
        [self initViewWithTitle:title message:message frame:frame];
        _block = block;
    }
    return self;
}

-(void)initViewWithTitle:(NSString *)title message:(NSString *)message frame:(CGRect)frame{
    self.frame = frame;
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 6;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
        make.width.mas_offset(300);
        make.height.mas_offset(174);
    }];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor colorWithHexString:@"000000"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = title;
    [bgView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(40);
    }];
    
    UILabel * contentLabel = [[UILabel alloc] init];
    contentLabel.textColor = [UIColor colorWithHexString:@"000000"];
    contentLabel.font = [UIFont systemFontOfSize:15];
    contentLabel.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:contentLabel];
    contentLabel.text = message;
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(7);
    }];
    
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = 6;
    cancelBtn.backgroundColor = [UIColor colorWithHexString:@"cd1212"];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [bgView addSubview:cancelBtn];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self removeFromSuperview];
    }];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(90);
        make.height.mas_offset(35);
        make.left.mas_equalTo(30);
        make.bottom.mas_equalTo(-36);
    }];
    
    
    UIButton * corfirmeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    corfirmeBtn.layer.masksToBounds = YES;
    corfirmeBtn.layer.cornerRadius = 6;
    corfirmeBtn.layer.borderWidth = 0.75;
    corfirmeBtn.layer.borderColor = [UIColor colorWithHexString:@"cd1212"].CGColor;
    [corfirmeBtn setTitleColor:[UIColor colorWithHexString:@"cd1212"] forState:UIControlStateNormal];
    [corfirmeBtn setTitle:@"确定" forState:UIControlStateNormal];
    corfirmeBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [bgView addSubview:corfirmeBtn];
    [[corfirmeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (self->_block) {
            self->_block();
        }
        [self removeFromSuperview];
    }];
    
    [corfirmeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(90);
        make.height.mas_offset(35);
        make.right.mas_equalTo(-30);
        make.bottom.mas_equalTo(-36);
    }];
    
    
}

@end
